package com.leyou.sms.listener;

import com.leyou.sms.utils.SendMsgUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.leyou.common.constants.MQConstants.Exchange.SMS_EXCHANGE_NAME;
import static com.leyou.common.constants.MQConstants.Queue.SMS_VERIFY_CODE_QUEUE;
import static com.leyou.common.constants.MQConstants.RoutingKey.VERIFY_CODE_KEY;

/**
 * @author syf
 * @version 1.0
 * @date 2019/08/12 16:12
 * @Description TODO:短信服务监听器
 */
@Slf4j
@Component
public class SmsListener {
    @Autowired
    private SendMsgUtils sendMsgUtils;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value =SMS_VERIFY_CODE_QUEUE  ,durable ="true"),
            exchange = @Exchange(value = SMS_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = VERIFY_CODE_KEY

    ))
    public void SendCheckMsgListener(Map<String,String> msg){

        try {
            if (msg==null) {
                return;
            }
            String phone = msg.get("phone");
            String code = msg.get("code");
            sendMsgUtils.sendCheckCodMsg(phone, code);

        }catch (Exception e){
            // 短信验证码失败后不重发，所以需要捕获异常。
            log.error("【SMS服务】短信验证码发送失败", e);
        }

    }
}
